from api import log
from config import HOST


class ApiRegisterLogin:
    #初始化
    def __init__(self,session):
        #获取session对象
        self.session=session
        #图片验证码URL
        self._url_img_code=HOST+"/common/public/verifycode1/{}"
        #短信验证码URL
        self._url_phone_code=HOST+"/member/public/sendSms"
        #注册接口URL
        self._url_register=HOST+"/member/public/reg"
        #登录接口URL
        self._url_login = HOST + "/member/public/login"
        #查询登陆状态接口URL
        self._url_login_status = HOST + "/member/public/islogin"
        #退出登录接口URL
        self._url_login_out=HOST+"/member/member/logout"


    #1、获取图片验证码接口 封装
    def api_img_code(self,random):
        log.info(f"正在调用获取图片验证码接口，请求方法：get 请求url：{self._url_img_code.format(random)} ")
        return self.session.get(url=self._url_img_code.format(random))


    #2、获取短信验证码接口 封装
    def api_phone_code(self,phone,imgVerifyCode):
        data={
            "phone":phone,
            "imgVerifyCode":imgVerifyCode,
            "type":"reg"
        }
        log.info(f"正在调用获取短信验证码接口，请求方法：post 请求url：{self._url_phone_code} 请求数据{data}")
        return self.session.post(url=self._url_phone_code,data=data)

    #3、注册接口 封装
    def api_register(self,phone,password,verifycode,phone_code):
        data={
            "phone":phone,
            "password":password,
            "verifycode":verifycode,
            "phone_code":phone_code,
            "dy_server":"on",
            "invite_phone":""
        }
        log.info(f"正在调用注册接口，请求方法：post 请求url：{self._url_register} 请求数据{data}")
        return self.session.post(url=self._url_register,data=data)


    #4、登录接口 封装
    def api_login(self,keywords,password):
        data={
            "keywords":keywords,
            "password":password
        }
        log.info(f"正在调用登录接口，请求方法：post 请求url：{self._url_login} 请求数据{data}")
        return self.session.post(url=self._url_login,data=data)


    #5、查询登陆状态接口 封装
    def api_login_status(self):
        log.info(f"正在调用查询登陆状态接口，请求方法：post 请求url：{self._url_login_status} ")
        return self.session.post(url=self._url_login_status)

    #6退出登录接口 封装
    def api_login_out(self):
        log.info(f"正在调用退出登录接口，请求方法：get 请求url：{self._url_login_out} ")
        return self.session.get(url=self._url_login_out)


